package com.ruoyi.purchase.controller;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.RoleUtils;
import com.ruoyi.purchase.domain.ProcOrder;
import com.ruoyi.purchase.service.IProcOrderService;
import com.ruoyi.basedata.service.IBaseSupplierService;
import com.ruoyi.basedata.domain.BaseSupplier;
import org.apache.commons.lang3.StringUtils;

/**
 * 采购订单审核Controller
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/purchase/audit")
public class ProcOrderAuditController extends BaseController
{
    private String prefix = "purchase/audit";

    @Autowired
    private IProcOrderService procOrderService;
    
    @Autowired
    private IBaseSupplierService baseSupplierService;

    @RequiresPermissions("purchase:audit:view")
    @GetMapping()
    public String audit()
    {
        return prefix + "/audit";
    }

    /**
     * 查询待审核的采购订单列表
     */
    @RequiresPermissions("purchase:audit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcOrder procOrder)
    {
        // 设置参数
        Map<String, Object> params = new HashMap<>();
        
        // 判断是否为审核员
        boolean isAuditor = RoleUtils.isAuditor();
        params.put("isAuditor", isAuditor);
        
        // 判断是否为总负责人
        boolean isChief = RoleUtils.isChief();
        params.put("isChief", isChief);
        
        // 总负责人只能看到审核通过的数据
        if (isChief) {
            procOrder.setAuditStatus("1"); // 审核通过
        }
        
        procOrder.setParams(params);
        
        startPage();
        List<ProcOrder> list = procOrderService.selectProcOrderList(procOrder);
        return getDataTable(list);
    }

    /**
     * 查看采购订单详情
     */
    @RequiresPermissions("purchase:audit:view")
    @GetMapping("/detail/{orderId}")
    public String detail(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        ProcOrder procOrder = procOrderService.selectProcOrderByOrderId(orderId);
        mmap.put("procOrder", procOrder);
        
        // 查询供应商信息
        BaseSupplier baseSupplier = new BaseSupplier();
        baseSupplier.setStatus("0");
        List<BaseSupplier> supplierList = baseSupplierService.selectBaseSupplierList(baseSupplier);
        mmap.put("supplierList", supplierList);
        
        return prefix + "/detail";
    }

    /**
     * 审核采购订单页面
     */
    @RequiresPermissions("purchase:audit:view")
    @GetMapping("/toAudit/{orderId}")
    public String toAudit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        ProcOrder procOrder = procOrderService.selectProcOrderByOrderId(orderId);
        mmap.put("procOrder", procOrder);
        return prefix + "/audit_form";
    }

    /**
     * 审核通过采购订单
     */
    @RequiresPermissions("purchase:audit:approve")
    @Log(title = "采购订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(Long orderId, String auditOpinion)
    {
        return toAjax(procOrderService.approveOrder(orderId, auditOpinion));
    }

    /**
     * 审核拒绝采购订单
     */
    @RequiresPermissions("purchase:audit:reject")
    @Log(title = "采购订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult reject(Long orderId, String auditOpinion)
    {
        return toAjax(procOrderService.rejectOrder(orderId, auditOpinion));
    }

    /**
     * 批量审核通过采购订单
     */
    @RequiresPermissions("purchase:audit:approve")
    @Log(title = "批量采购订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/batchApprove")
    @ResponseBody
    public AjaxResult batchApprove(String ids, String auditOpinion)
    {
        if (StringUtils.isEmpty(auditOpinion)) {
            auditOpinion = "批量审核通过";
        }
        int successCount = procOrderService.batchApproveOrders(ids, auditOpinion);
        return AjaxResult.success("成功审核通过 " + successCount + " 个订单");
    }

    /**
     * 批量审核拒绝采购订单
     */
    @RequiresPermissions("purchase:audit:reject")
    @Log(title = "批量采购订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/batchReject")
    @ResponseBody
    public AjaxResult batchReject(String ids, String auditOpinion)
    {
        if (StringUtils.isEmpty(auditOpinion)) {
            auditOpinion = "批量审核不通过";
        }
        int successCount = procOrderService.batchRejectOrders(ids, auditOpinion);
        return AjaxResult.success("成功审核拒绝 " + successCount + " 个订单");
    }
} 